﻿using System;
using System.Text;
using AchSegítő.extensions;

namespace AchSegítő.File
{
    /// <summary>
    /// Where did it come from and where is it going?
    /// </summary>
    public class FileHeader
    { 
        /// <summary>
        /// THIS IS THE FIRST POSITION OF ALL RECORD FORMATS. THE CODE IS UNIQUE FOR EACH RECORD TYPE.
        /// THE FILE HEADER RECORD USED RECORD TYPE CODE 1.
        /// </summary>
        public readonly int RecordTypeCode = (int)RecordTypeCodes.FileHeader;

        /// <summary>
        /// PRIORITY CODES ARE NOT USED AT THIS TIME; THIS FIELD CONTAIN 01.
        /// </summary>
        public int PriorityCode { get; set; } = 1;

        /// <summary>
        /// ENTER YOUR PNC BANK TRANSIT/ROUTING NUMBER PRECEDED BY A BLANK SPACE I.E. B999999999.
        /// </summary>
        public String ImmediateDestination { get; set; } = String.Empty;

// ACHBankRoutingNumber

        /// <summary>
        /// THIS FIELD IDENTIFIES THE ORGANIZATION OR COMPANY ORIGINATING THE
        /// FILE. THE FIELD BEGINS WITH A NUMBER, USUALLY '1' AND THE ORIGINATOR'S
        /// 9-DIGIT TAX ID WILL FOLLOW. IF THE FIELD CANNOT BE POPULATED WITH 10 DIGITS,
        /// A BLANK AND 9 DIGITS CAN BE USED.
        /// </summary>
        public String ImmediateOrigin { get; set; } = String.Empty;

        /// <summary>
        /// DATE WHEN THE ORIGINATOR CREATED THE FILE. THE DATE MUST BE IN "YYMMDD" FORMAT.
        /// </summary>
        public DateTime FileCreationDateTime { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// THIS PROVIDES A MEANS FOR AN ORIGINATOR TO DISTINGUISH BETWEEN MULTIPLE FILES CREATED ON THE SAME DATE. ONLY UPPERCASE, A-Z AND NUMBERS, 0-9 ARE PERMITTED.
        /// </summary>
        public String FileIDModifier { get; set; } = "A";

        /// <summary>
        /// THIS FIELD INDICATES THE NUMBER OF CHARACTERS CONTAINED IN EACH RECORD. THE VALUE 094 IS USED.
        /// </summary>
        public readonly int RecordSize = 94;

        /// <summary>
        /// THIS BLOCKING FACTOR DEFINES THE NUMBER OF PHYSICAL RECORDS WITHIN A FILE. THE VALUE 10 MUST BE USED.
        /// </summary>
        public readonly int BlockingFactor = 10;

        /// <summary>
        /// THIS FIELD MUST CONTAIN 1.
        /// </summary>
        public readonly int FormatCode = 1;

        /// <summary>
        /// The bank name that you're sending to, i.e. "FirstMerit/Huntinton BANK"
        /// </summary>
        public String ImmediateDestinationName { get; set; } = String.Empty;

// ACHFileBankName

        /// <summary>
        /// THIS FIELD IDENTIFIES THE ORIGINATOR OF THE FILE. THE NAME OF THE ORIGINATING COMPANY SHOULD BE USED.
        /// </summary>
        public String ImmediateOriginName { get; set; } = String.Empty;

        /// <summary>
        /// BLANKS FILL THIS FIELD.
        /// </summary>
        public readonly String ReferenceCode = String.Empty;

        public override string ToString()
        {
            var sb = new StringBuilder(ACH.CHARACTERS_PER_LINE);

            sb.Append((int)RecordTypeCode);

            sb.Append(PriorityCode.ToString().TrimAndPadLeft(2, '0'));

            sb.Append(ImmediateDestination.TrimAndPadLeft(10));

            sb.Append(ImmediateOrigin.TrimAndPadRight(10));

            sb.Append(FileCreationDateTime.ToString("yyMMdd"));

            sb.Append(FileCreationDateTime.ToString("HHmm"));

            sb.Append(FileIDModifier.TrimAndPadRight(1));

            sb.Append(RecordSize.ToString().TrimAndPadLeft(3, '0'));

            sb.Append(BlockingFactor);

            sb.Append(FormatCode);

            sb.Append(ImmediateDestinationName.TrimAndPadRight(23));

            sb.Append(ImmediateOriginName.TrimAndPadRight(23));

            sb.Append(ReferenceCode.TrimAndPadRight(8));

            sb.AppendLine();

            return sb.ToString();
        }
    }
}